<template>
  <div>
    <br>
    <el-form :inline="true">
      <el-form-item label="房源编号">
        <el-input v-model="searchMap.house_id" placeholder="房源编号"></el-input>
      </el-form-item>
      <br>
      <el-form-item label="房源配套 ">
        <!--      <el-checkbox-group v-model="checkList">-->

        <el-checkbox label="电视" v-model="searchMap.television" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="冰箱" v-model="searchMap.refrigerator" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="洗衣机" v-model="searchMap.washer" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="空调" v-model="searchMap.air_conditioner" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="热水器" v-model="searchMap.calorifier" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="床" v-model="searchMap.bed" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="暖气" v-model="searchMap.heating" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="宽带" v-model="searchMap.broadband" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="衣柜" v-model="searchMap.closet" true-label="有" false-label="无"></el-checkbox>
        <el-checkbox label="天然气" v-model="searchMap.natgas" true-label="有" false-label="无"> </el-checkbox>
        <!--      </el-checkbox-group>-->
      </el-form-item>
      <br>

      <!--<el-form-item label="电视">
<el-input v-model="searchMap.television" placeholder="电视"></el-input></el-form-item>
      <el-form-item label="冰箱">
<el-input v-model="searchMap.refrigerator" placeholder="冰箱"></el-input></el-form-item>
      <el-form-item label="洗衣机">
<el-input v-model="searchMap.washer" placeholder="洗衣机"></el-input></el-form-item>
      <el-form-item label="空调">
<el-input v-model="searchMap.air_conditioner" placeholder="空调"></el-input></el-form-item>
      <el-form-item label="热水器">
<el-input v-model="searchMap.calorifier" placeholder="热水器"></el-input></el-form-item>
      <el-form-item label="床">
<el-input v-model="searchMap.bed" placeholder="床"></el-input></el-form-item>
      <el-form-item label="暖气">
<el-input v-model="searchMap.heating" placeholder="暖气"></el-input></el-form-item>
      <el-form-item label="宽带">
<el-input v-model="searchMap.broadband" placeholder="宽带"></el-input></el-form-item>
      <el-form-item label="衣柜">
<el-input v-model="searchMap.closet" placeholder="衣柜"></el-input></el-form-item>
      <el-form-item label="天然气">
<el-input v-model="searchMap.natgas" placeholder="天然气"></el-input></el-form-item>-->

      <el-button type="primary" @click="fetchData()">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>
    <el-table
      :data="list"
      border
      style="width: 100%">
      <el-table-column prop="id" label="编号" width="200"></el-table-column>
      <el-table-column prop="house_id" label="房源编号" width="200"></el-table-column>
      <el-table-column prop="television" label="电视" width="80"></el-table-column>
      <el-table-column prop="refrigerator" label="冰箱" width="80"></el-table-column>
      <el-table-column prop="washer" label="洗衣机" width="80"></el-table-column>
      <el-table-column prop="air_conditioner" label="空调" width="80"></el-table-column>
      <el-table-column prop="calorifier" label="热水器" width="80"></el-table-column>
      <el-table-column prop="bed" label="床" width="80"></el-table-column>
      <el-table-column prop="heating" label="暖气" width="80"></el-table-column>
      <el-table-column prop="broadband" label="宽带" width="80"></el-table-column>
      <el-table-column prop="closet" label="衣柜" width="80"></el-table-column>
      <el-table-column prop="natgas" label="天然气" width="80"></el-table-column>

      <el-table-column

        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)"  size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row.id)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="fetchData"
      @current-change="fetchData"
      :current-page.sync="currentPage"
      :page-sizes="[5,10,20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-form-item label="房源编号">
          <el-input v-model="pojo.house_id"></el-input>
        </el-form-item>

        <el-form-item label="房源配套 ">
          <!--      <el-checkbox-group v-model="checkList">-->

          <el-checkbox label="电视" v-model="pojo.television" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="冰箱" v-model="pojo.refrigerator" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="洗衣机" v-model="pojo.washer" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="空调" v-model="pojo.air_conditioner" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="热水器" v-model="pojo.calorifier" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="床" v-model="pojo.bed" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="暖气" v-model="pojo.heating" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="宽带" v-model="pojo.broadband" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="衣柜" v-model="pojo.closet" true-label="有" false-label="无"></el-checkbox>
          <el-checkbox label="天然气" v-model="pojo.natgas" true-label="有" false-label="无"></el-checkbox>
          <!--      </el-checkbox-group>-->
        </el-form-item>

        <el-button type="primary" @click="handleSave()">保存</el-button>
        <el-button @click="dialogFormVisible = false">关闭</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import productApi from '@/api/product'

  export default {
    data() {
      return {
        checkList: [],
        list: [],
        total: 0, // 总记录数
        currentPage: 1, // 当前页
        pageSize: 10, // 每页大小
        searchMap: {}, // 查询条件
        dialogFormVisible: false, // 编辑窗口是否可见
        pojo: {}, // 编辑表单绑定的实体对象
        cityList: [], // 城市列表
        id: '' // 当前用户编辑的ID
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        productApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
          this.list = response.data.rows
          this.total = response.data.total
        })
      },
      handleSave() {
        productApi.update(this.id, this.pojo).then(response => {
          this.$message({
            message: response.message,
            type: (response.flag ? 'success' : 'error')
          })
          if (response.flag) { // 如果成功
            this.fetchData() // 刷新列表
          }
        })
        this.dialogFormVisible = false // 关闭窗口
      },
      handleEdit(id) {
        this.id = id
        this.dialogFormVisible = true // 打开窗口
        if (id !== '') { // 编辑
          productApi.findById(id).then(response => {
            if (response.flag) {
              this.pojo = response.data
            }
          })
        } else {
          this.pojo = {} // 清空数据
        }
      },
      handleDelete(id) {
        this.$confirm('确定要删除此纪录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          productApi.deleteById(id).then(response => {
            this.$message({message: response.message, type: (response.flag ? 'success' : 'error')})
            if (response.flag) {
              this.fetchData() // 刷新数据
            }
          })
        })
      }
    }
  }
</script>
